<template>
  <el-card>
    <el-form :inline="true" :model="form">
      <el-form-item>
        <el-input
          v-model="form.order_no"
          :placeholder="$t('订单号搜索')"
          clearable
        />
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.currency"
          :placeholder="$t('币种')"
          clearable
          class="w-24"
        >
          <el-option
            v-for="item in currencyList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.mer_review_status"
          :placeholder="$t('商户审核状态')"
          clearable
          class="w-36"
        >
          <el-option
            v-for="item in merList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="form.sys_review_status"
          :placeholder="$t('平台审核状态')"
          clearable
          class="w-36"
        >
          <el-option
            v-for="item in sysList"
            :key="item.key"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="form.timeData"
          type="datetimerange"
          :range-separator="$t('至')"
          :start-placeholder="$t('开始日期')"
          :end-placeholder="$t('截止日期')"
          :default-time="pickerDefaultTime"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" :icon="Search" @click="onSubmit">{{
          $t("查询")
        }}</el-button>
        <el-button
          type="primary"
          :icon="Plus"
          @click="handleClick"
          v-btn="['all', 'web_withdraw_add']"
        >
          {{ $t("新增提现") }}
        </el-button>
        <el-button
          type="success"
          :icon="CreditCard"
          @click="handleClickCollectionCard"
          v-btn="['all', 'web_withdraw_manage_cards']"
        >
          {{ $t("银行卡管理") }}
        </el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column
        prop="create_time"
        :label="$t('创建时间')"
        align="center"
        :formatter="formatterTime"
      />
      <el-table-column :label="$t('提现金额')" align="right">
        <template #default="{ row }">
          {{ numberToCurrencyNo(row.amount) }}
        </template>
      </el-table-column>
      <el-table-column prop="currency" :label="$t('币种')" align="center" />
      <el-table-column
        prop="status_name"
        :label="$t('订单状态')"
        align="center"
      />
      <el-table-column
        prop="sys_review_status_name"
        :label="$t('平台审核状态')"
        align="center"
      />
      <el-table-column
        prop="mer_review_status_name"
        :label="$t('商户审核状态')"
        align="center"
      />
      <el-table-column prop="order_no" :label="$t('订单号')" align="center" />
      <el-table-column
        :label="$t('操作')"
        align="center"
        fixed="right"
        width="140"
      >
        <template #default="scope">
          <el-button
            round
            :icon="MoreFilled"
            v-btn="['all', 'web_withdraw_view']"
            class="cursor-pointer"
            @click="handleDetails(scope.row)"
            >{{ $t("查看") }}</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="mt-5 flex justify-center">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageable.total"
        :default-page-size="pageable.pageSize"
        :default-current-page="pageable.pageNum"
        :page-sizes="[10, 25, 50, 100]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
  <WithdrawalapplicationForm
    @closeDialog="closeDialog"
    @submitForm="dialogSubmit"
    @handleGoAddCard="handleGoAddCard"
    :dialogVisible="dialogVisible"
    :currencyList="currencyList"
    :cardsList="cardsList"
  />
</template>
<script setup>
import { ref, reactive } from "vue";
import {
  withdrawListApi,
  GetCurrencyApi,
  withdrawSubmitApi,
  cardsListApi,
} from "@/api/account/index";
import { useRouter } from "vue-router";
import WithdrawalapplicationForm from "./components/withdrawalapplicationForm.vue";
import {
  timestamp,
  formatterTime,
  numberToCurrencyNo,
  pickerDefaultTime,
} from "@/utils/utils";
import { ElMessage } from "element-plus";
import { Search, MoreFilled, Plus, CreditCard } from "@element-plus/icons-vue";
import { useMerchantStore } from "@/store/modules/merchant";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const merchantStore = useMerchantStore();
const loading = ref(false);
const router = useRouter();
const dialogVisible = ref(false);
const form = reactive({
  currency: "",
  timeData: [],
});

const currencyList = ref([]); //币种
const cardsList = ref([]); //银行卡列表
//商户审核状态
const merList = ref([]);
//系统审核状态
const sysList = ref([]);

// 分页
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
// 表格数据
const tableData = ref([]);
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    currency: form.currency,
    pagesize: pageable.pageSize,
    mer_review_status: form.mer_review_status,
    sys_review_status: form.sys_review_status,
    order_no: form.order_no,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
const onSubmit = () => {
  getTbaleData();
};
const handleDetails = ({ ...row }) => {
  router.replace({
    path: "/account/withdraw/details",
    query: { order_no: row.order_no },
  });
};
// 收款卡
const handleClickCollectionCard = () => {
  router.replace(`/account/withdraw/card`);
};
const handleGoAddCard = () => {
  router.replace({
    path: "/account/withdraw/card",
  });
};
// 获取银行卡
const getCardsData = async () => {
  const res = await cardsListApi();
  if (res?.list.length) {
    cardsList.value = res.list.map((item) => {
      return {
        valie: item.card_id,
        label: `${t("收款银行账号")} - ${item.card_number}`,
        ...item,
      };
    });
  }
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 打开窗口
const handleClick = () => {
  getCardsData();
  dialogVisible.value = true;
};
// 关闭窗口
const closeDialog = () => {
  dialogVisible.value = false;
};
// 提交
const dialogSubmit = async (param) => {
  // console.log(param);
  try {
    const res = await withdrawSubmitApi(param);
    ElMessage({
      showClose: true,
      message: "success",
      type: "success",
    });
    dialogVisible.value = false;
    getTbaleData();
  } catch (error) {
    dialogVisible.value = false;
  }
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    // 获取数据
    const res = await withdrawListApi(param());
    merList.value = res.extra.filter_mer_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    sysList.value = res.extra.filter_sys_review_status.map((item) => {
      return { value: item.value, label: item.name };
    });
    loading.value = false;
    tableData.value = res.list;
    pageable.total = res.page.total_count;
  } catch (error) {
    loading.value = false;
  }
};
// 获取币种
const getCurrencyData = async () => {
  const res = await GetCurrencyApi();
  if (res.length) {
    currencyList.value = res.map((item) => {
      return { value: item, label: item };
    });
  }
};

Promise.all([
  getTbaleData(),
  getCurrencyData(),

  merchantStore.getMerchantInfo(),
]);
</script>
